<template>
	<view class="page">
		<uni-notice-bar text="如需其他单位协调处理选择安质部" />

		<view class="allot-form">
			<uni-forms :modelValue="formData" :rules="formRules" ref="formRef" label-position="top" label-width="150">
				<uni-forms-item required name="allot_dept" label="分配部门"><uni-data-select v-model="formData.allot_dept" :localdata="deptList"></uni-data-select></uni-forms-item>
				<uni-forms-item name="allot_remark" label="分配备注"><uni-easyinput type="text" v-model="formData.allot_remark" placeholder="请输入分配备注" /></uni-forms-item>
			</uni-forms>

			<button @tap="submitAllocate" class="allot-submit" hover-class="hover-class">{{ $t('common.button.allot') }}</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			formData: {
				allot_remark: '',
				allot_dept: ''
			},
			deptList: [],
			formRules: {
				allot_dept: {
					rules: [
						{
							required: true,
							errorMessage: '请选择分配部门'
						}
					]
				}
			}
		}
	},
	computed: {},
	onLoad(options) {
		this.questionId = options.id || 0
		this.getListOneOffice()
	},
	onReady() {
		this.$refs.formRef.setRules(this.formRules)
	},
	methods: {
		async getListOneOffice() {
			try {
				const res = await uni.$uu.Api.getListOneOffice()

				console.log(res)

				if (res.code === 100) {
					this.deptList = res.content.map(_ => {
						return {
							text: _.name,
							value: _.id
						}
					})
				}
			} catch (e) {
				console.log(e)
			}
		},
		submitAllocate(e) {
			console.log('submitAllocate:', this.formData)
			this.$refs.formRef
				.validate()
				.then(res => {
					console.log('表单数据信息：', res)
					this.allocateDept()
				})
				.catch(err => {
					console.log('表单错误信息：', err)
				})
		},
		async allocateDept() {
			const params = {
				questionId: this.questionId,
				dutyOfficeId: this.formData.allot_dept,
				remark: this.formData.allot_remark
			}

			try {
				const res = await uni.$uu.Api.appointQuestion(params)

				console.log(res)

				if (res.code === 100) {
					uni.showToast({
						title: '分配成功',
						success: () => {
							// const pages = getCurrentPages()
							// if (pages.length > 1) {
							// 	const prevPage = pages[pages.length - 2]
							// 	prevPage.$vm.getData(1)
							// }
							uni.navigateBack()
						}
					})
				}
			} catch (err) {
				console.log(err)
			}
		}
	}
}
</script>

<style lang="scss" scoped>
@import '~@/common/app.scss';
@import './index.scss';
</style>
